package com.domain.controller;

import com.domain.common.Result;
import com.domain.utils.PropertiesParse;
import com.domain.utils.UUIDUtils;
import com.domain.utils.UploadFileUntil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;

@Controller
@RequestMapping("/upload")
public class UploadController {

    /**
     * 腾讯服务器上传文件(单个文件)
     */
    /**
     * 腾讯服务器上传文件
     */
    @RequestMapping("/txUploadImg")
    @ResponseBody
    public Result txUploadImg(HttpServletRequest request) {
        try {
            String path = PropertiesParse.getConfigPropertiesValue("tx_domain_name");
            //取得文件
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");

            //原来文件名
            String filename = file.getOriginalFilename();
            //文件类型
            String fileType = filename.substring(filename.lastIndexOf(".") + 1);
            //新文件名
            String name = System.currentTimeMillis() + UUIDUtils.VerifyCode(6);

            InputStream inputStream = file.getInputStream();

            String etag = UploadFileUntil.UploadImg(inputStream, name, fileType);

            if (etag != null) {
                return Result.success(path + name);
            } else {
                return Result.fail("系统故障,请稍后再试");
            }
        } catch (Exception e) {
            return Result.fail("系统故障,请稍后再试");
        }
    }
    

}
